package com.amt.olcacon.entity;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 透视表数据容器
 * 行：flowName
 * 列：impactCategoryName  
 * 值：result
 */
public class PivotTable {
    private final Map<String, Map<String, Double>> tableData;
    private final List<String> rowNames;
    private final List<String> columnNames;
    
    public PivotTable(Map<String, Map<String, Double>> tableData) {
        this.tableData = tableData;
        this.rowNames = new ArrayList<>(tableData.keySet());
        this.columnNames = extractColumnNames(tableData);
        
        // 排序
        Collections.sort(rowNames);
        Collections.sort(columnNames);
    }
    
    private List<String> extractColumnNames(Map<String, Map<String, Double>> tableData) {
        return tableData.values().stream()
                .flatMap(map -> map.keySet().stream())
                .distinct()
                .collect(Collectors.toList());
    }
    
    public Double getValue(String flowName, String impactCategoryName) {
        Map<String, Double> row = tableData.get(flowName);
        return row != null ? row.get(impactCategoryName) : null;
    }
    
    public Map<String, Double> getRow(String flowName) {
        return tableData.get(flowName);
    }
    
    public List<String> getRowNames() {
        return new ArrayList<>(rowNames);
    }
    
    public List<String> getColumnNames() {
        return new ArrayList<>(columnNames);
    }
    
    public Map<String, Map<String, Double>> getTableData() {
        return Collections.unmodifiableMap(tableData);
    }
    
    public int getRowCount() {
        return rowNames.size();
    }
    
    public int getColumnCount() {
        return columnNames.size();
    }
}